package com.hopedove.quanlitycheck.utils;

import android.content.Context;
import android.content.SharedPreferences;


public class QualitySharedPrefUtil {
    /**
     * 用户登录信息
     **/
    public static final String USERBEAN = "USERBEAN";

    /**
     * 用户编号
     **/
    public static final String YHBH = "YHBH";
    /**
     * 用户名称
     **/
    public static final String YHMC = "YHMC";

    public static final String DATASUM = "DATASUM";
    /***用户姓名**/
    public static final String XM = "XM";
    /**
     * 角色名称
     **/
    public static final String JSMC = "JSMC";

    private static final String SHARED_PREFS_NAME = "quality_module";

    public static void putValue(Context context, String key, Object value) {
        String type = value.getClass().getSimpleName();
        SharedPreferences sharedPreferences = context.getSharedPreferences(SHARED_PREFS_NAME, 0);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        if ("Integer".equals(type)) {
            editor.putInt(key, (Integer) value);
        } else if ("Boolean".equals(type)) {
            editor.putBoolean(key, (Boolean) value);
        } else if ("String".equals(type)) {
            editor.putString(key, (String) value);
        } else if ("Float".equals(type)) {
            editor.putFloat(key, (Float) value);
        } else if ("Long".equals(type)) {
            editor.putLong(key, (Long) value);
        }
        editor.apply();
    }

    public static Object getValue(Context context, String key, Object defValue) {
        String type = defValue.getClass().getSimpleName();
        SharedPreferences sharedPreferences = context.getSharedPreferences(SHARED_PREFS_NAME, 0);
        //defValue为为默认值，如果当前获取不到数据就返回它
        if ("Integer".equals(type)) {
            return sharedPreferences.getInt(key, (Integer) defValue);
        } else if ("Boolean".equals(type)) {
            return sharedPreferences.getBoolean(key, (Boolean) defValue);
        } else if ("String".equals(type)) {
            return sharedPreferences.getString(key, (String) defValue);
        } else if ("Float".equals(type)) {
            return sharedPreferences.getFloat(key, (Float) defValue);
        } else if ("Long".equals(type)) {
            return sharedPreferences.getLong(key, (Long) defValue);
        }
        return null;
    }
}
